class Solution {
public:
    int pivotIndex(vector<int>& nums) {
        int n = nums.size();
        vector<int>f(n + 1, 0), temp(n + 1, 0);
        int idx = 1;
        for (auto x : nums) {
            temp[idx++] = x;
        }
        for (int i = 1; i <= n; i++) {
            f[i] = f[i - 1] + temp[i];
        }
        for (int i = 1; i <= n; i++) {
            if (f[i - 1] == f[n] - f[i]) {
                return i - 1;
            }
        }
        return -1;
    }
};